﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LoginCommand.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="LoginCommand" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Desktop.Commands
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows.Input;

    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Desktop.ViewModel;
    using Hisaab.Client.Model;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="LoginCommand"/> type.
    /// </summary>
    public class LoginCommand : ICommand
    {
        /// <summary>
        /// User manager object
        /// </summary>
        private readonly IUserManager _userManager;

        /// <summary>
        /// Member variable _viewModel
        /// </summary>
        private readonly LoginViewModel _viewModel;

        /// <summary>
        /// Initializes a new instance of the <see cref="LoginCommand"/> class.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        public LoginCommand(LoginViewModel viewModel)
            : this(viewModel, ServiceLocator.Current.GetInstance<IUserManager>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LoginCommand"/> class.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="userManager">The user manager.</param>
        public LoginCommand(LoginViewModel viewModel, IUserManager userManager)
        {
            Contract.Requires<ArgumentNullException>(viewModel != null);
            Contract.Requires<ArgumentNullException>(userManager != null);

            _viewModel = viewModel;
            _userManager = userManager;
        }

        #region Implementation of ICommand

        /// <summary>
        /// Occurs when changes occur that affect whether or not the command should execute.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            if (parameter is User)
            {
                var user = parameter as User;
                if (_userManager.ValidateUser(user.UserName, user.Password) >= 0)
                {
                    _viewModel.CloseView();
                }
            }
        }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public bool CanExecute(object parameter)
        {
            if (parameter is User)
            {
                var user = parameter as User;
                return !string.IsNullOrWhiteSpace(user.UserName);
            }

            return false;
        }

        #endregion Implementation of ICommand
    }
}